package com.chinaoly.baotou.common.utils;

import com.chinaoly.frm.core.entity.NewRuntimException;
import com.chinaoly.frm.core.entity.ShiroUser;
import lombok.extern.slf4j.Slf4j;

import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Date;

/**
 * @author xuyh
 * @date 2021/9/16
 */
@Slf4j
public class EntityUtil {

    private static final String[] COMMON_FIELD = {"id", "cjrid", "cjsj", "cjrmc", "cjbmdm", "cjbmmc"};

    /**
     * 对通用字段进行赋值
     * 此项目中通用字段：id, cjrid, cjsj, cjrmc, cjbmdm, cjbmmc
     *
     * @param o    object
     * @param user 当前用户
     */
    public static void setCommonValue(Object o, ShiroUser user) {
        if (o instanceof Iterable) {
            throw new NewRuntimException("暂不支持Iterable对象转换");
        }
        //获得该类的所有属性
        Field[] fields = o.getClass().getDeclaredFields();
        Arrays.stream(fields).forEach(field -> {
            if (Arrays.stream(COMMON_FIELD).anyMatch(str -> str.equals(field.getName()))) {
                PropertyDescriptor propertyDescriptor = null;
                try {
                    propertyDescriptor = new PropertyDescriptor(field.getName(), o.getClass());
                } catch (IntrospectionException e) {
                    log.error("未找到对应方法：", e);
                    throw new NewRuntimException("系统内部错误!");
                }
                //获得set方法
                Method method = propertyDescriptor.getWriteMethod();
                try {
                    switch (field.getName()) {
                        case "id":
                            method.invoke(o, ObjectId.next());
                            break;
                        case "cjrid":
                            if (user != null) {
                                method.invoke(o, user.getId());
                            }
                            break;
                        case "cjsj":
                            method.invoke(o, new Date());
                            break;
                        case "cjrmc":
                            if (user != null) {
                                method.invoke(o, user.getUserName());
                            }
                            break;
                        case "cjbmdm":
                            if (user != null) {
                                method.invoke(o, user.getOfficeCode());
                            }
                            break;
                        case "cjbmmc":
                            if (user != null) {
                                method.invoke(o, user.getOfficeName());
                            }
                            break;
                        default:
                            break;
                    }
                } catch (InvocationTargetException e) {
                    log.error("反射异常：", e);
                    throw new NewRuntimException("系统内部错误!");
                } catch (IllegalAccessException e) {
                    log.error("没有该类的访问权限：", e);
                    throw new NewRuntimException("系统内部错误!");
                }
            }
        });
    }

}
